package com.dswz.controller.pc;

import com.dswz.po.Goods;
import com.dswz.service.GoodsService;
import com.dswz.service.SessionService;
import com.dswz.utils.AdminResult;
import com.dswz.utils.PageBean;
import com.dswz.utils.RequestUtils;
import com.dswz.utils.Utils;
import com.google.gson.Gson;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/pc" })
public class GoodsPcController {
	private Logger logger = LogManager.getLogger(GoodsPcController.class);

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private SessionService sessionService;

	@RequestMapping(value = { "/getGoodsList" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object getGoodsList(Byte goodsType, Integer page, Integer rows, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("goodsType:" + goodsType + "page:" + page + "limit:" + rows + "页面来源:"
					+ request.getHeader("referer") + "客户端:" + request.getHeader("user-agent") + " 客户端ip: "
					+ Utils.getUserRealAddress(request) + " 操作人: " + username);
			PageBean pb = this.goodsService.getGoodsList(username, goodsType, null, null, Byte.valueOf((byte) 0), page,
					rows);
			result.setResult(0);
			result.setMessage("OK");
			result.setRows(pb.getContentList());
			result.setTotal(pb.getTotalRecords());
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			result.setResult(2);
			result.setMessage(e.getMessage());
			this.logger.error(e.getMessage(), e);
		}
		return result;
	}

	@RequestMapping(value = { "/editGoods" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object editGoods(String goodsId, Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("goodsId:" + goodsId + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			Goods goods = this.goodsService.getGoodsById(goodsId);
			result.setData(goods);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}
}
